package ru.hh.team02.chat.messaging;

import java.util.Set;

import ru.hh.team02.chat.client.Client;
import ru.hh.team02.chat.server.ChatServer;

/**
 * class responsible for macro sending logic. current version - one queue with
 * messages per active client
 */
public class SendingService {

    /**
     * send personal message
     * @param message - message text
     * @param recepientName
     */
     public void sendPersonalMessage(String message, String recepientName) {
         if (!ChatServer.REGISTRY.hasClientWithName(recepientName)) {
             return;
         }
         
         Client recepient = ChatServer.REGISTRY.getClientByName(recepientName);
         recepient.add(new Message(message, true));
     }

    /**
     * Put message to all personal queues of people, who are in the same room as
     * sender
     * 
     * @param text
     *            Message text
     * @param name
     *            Sender name
     */
    public void sendMessageToRoom(String text, String senderName) {
        String room = ChatServer.REGISTRY.getClientRoom(senderName);
        Set<Client> recepients = ChatServer.REGISTRY.getClientsFromRoom(room);

        for (Client recepient : recepients) {
            recepient.add(new Message(text, false));
        }

    }
}
